//
//  KSTools.swift
//  Supermarket
//
//  Created by liuxm on 2017/8/8.
//  Copyright © 2017年 kushao. All rights reserved.
//

import UIKit
import SDWebImage
import WebKit

class KSTools: NSObject {
    
    /// 自适应Label高度
    ///
    /// - Parameters:
    ///   - width: 宽度
    ///   - title: 字符串
    ///   - size: 字体大小
    /// - Returns: 字体长度
    class func getHeightByWidth(_ width:CGFloat, title:String, size:CGFloat) ->CGFloat {
        let font = UIFont.systemFont(ofSize: size)
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: 0))
        label.text = title
        label.font = font
        label.numberOfLines = 0;
        label.sizeToFit()
        let height = label.frame.size.height;
        
        return height;
    }

    /// 自适应Label宽度
    ///
    /// - Parameters:
    ///   - title: 字符串
    ///   - size: 字体大小
    /// - Returns: 宽度
    class func getWidthWithTitle(_ title:String, size:CGFloat) ->CGFloat {
        let font = UIFont.systemFont(ofSize: size)
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: 1000, height: 0))
        label.text = title;
        label.font = font;
        label.sizeToFit()
        return label.frame.size.width;
    }
    
    /// 手机号码11位的数字必须全局匹配，支持13*，14*, 15*，17*, 18*号段
    ///
    /// - Parameter number: 手机号码
    /// - Returns: true/false
    class func checkPhoneNum(number:String) ->Bool {
        if !number.isEmpty {
            let numRegex = "^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\\d{8}$"
            let regextestmobile = NSPredicate(format: "SELF MATCHES %@" ,numRegex)
            return regextestmobile.evaluate(with:number)
        }
        
        return false
    }
    
    /// 颜色转图片
    ///
    /// - Parameter color: 颜色
    /// - Returns: 图片
    class func createImageWithColor(color: UIColor) -> UIImage {
        let rect: CGRect = CGRect(x: 0, y: 0, width: 1.0, height: 1.0)
        UIGraphicsBeginImageContext(rect.size)
        let context: CGContext = UIGraphicsGetCurrentContext()!
        context.setFillColor(color.cgColor)
        context.fill(rect)
        
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsGetCurrentContext()
        return image!
    }
    
    /// 退出登录更新登录信息
    class func exitUpdateLoginInfo() {
        KSUserDefaultsTools.setUserToken("")
        KSUserDefaultsTools.setUserHaveLogined(false)
        KSUserDefaultsTools.setUserMobile("")
    }
    
    class func telephone(phone: String) {
        UIApplication.shared.openURL(URL(string: "telprompt://" + phone)!)
    }
    
    // 获取缓存
    class func getCache() -> String {
        let size: CGFloat = CGFloat(SDImageCache.shared().getSize())
        var cacheString = ""
        if size > pow(10, 9) {
            cacheString = String.init(format: "%.2fGB", 1.0 * size/1024/1024/1024)
        } else if size > pow(10, 6) {
            cacheString = String.init(format: "%.2fMB", 1.0 * size/1024/1024)
        } else if size > pow(10, 3) {
            cacheString = String.init(format: "%.2fKB", 1.0 * size/1024)
        } else {
            cacheString = "0KB"
        }
        return cacheString
    }
    
    // 清除缓存
    class func clearCache() {
        SDImageCache.shared().clearDisk(onCompletion: nil)
        let websiteDataTypes = NSSet.init(array: [WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache]) as! Set<String>
        let dateFrom = Date.init(timeIntervalSince1970: 0)
        WKWebsiteDataStore.default().removeData(ofTypes: websiteDataTypes, modifiedSince: dateFrom) {
        }
    }
    
    /// 有订单状态获取订单的描述
    ///
    /// - Parameter status: 状态
    class func getOrderDescriptionByStatus(status: Int) -> String {
        var descp = ""
        switch status {
        case -2:
            descp = "已取消"
        case -1:
            descp = "审核拒绝"
        case 1:
            descp = "待供应商审核"
        case 2:
            descp = "已审核"
        case 3:
            descp = "待拣货"
        case 4:
            descp = "拣货完成"
        case 5:
            descp = "分拣完成"
        case 6:
            descp = "待装车"
        case 7:
            descp = "已发货"
        case 8:
            descp = "已验收"
        default:
            break
        }
        
        return descp
    }
}
